import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { useIntl } from '@ant-design/pro-provider';
import { parseValueToMoment } from '@ant-design/pro-utils';
import { DatePicker, Tooltip } from 'antd-v4';
import moment from 'moment';
/**
 * 与当前的时间进行比较 http://momentjs.cn/docs/displaying/fromnow.html
 *
 * @param
 */

var FieldFromNow = function FieldFromNow(_ref) {
  var text = _ref.text,
      mode = _ref.mode,
      render = _ref.render,
      renderFormItem = _ref.renderFormItem,
      format = _ref.format,
      fieldProps = _ref.fieldProps;
  var intl = useIntl();

  if (mode === 'read') {
    var dom = _jsx(Tooltip, {
      title: moment(text).format((fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.format) || format || 'YYYY-MM-DD HH:mm:ss'),
      children: moment(text).fromNow()
    });

    if (render) {
      return render(text, _objectSpread({
        mode: mode
      }, fieldProps), _jsx(_Fragment, {
        children: dom
      }));
    }

    return _jsx(_Fragment, {
      children: dom
    });
  }

  if (mode === 'edit' || mode === 'update') {
    var placeholder = intl.getMessage('tableForm.selectPlaceholder', '请选择');
    var momentValue = parseValueToMoment(fieldProps.value);

    var _dom = _jsx(DatePicker, _objectSpread(_objectSpread({
      placeholder: placeholder,
      showTime: true
    }, fieldProps), {}, {
      value: momentValue
    }));

    if (renderFormItem) {
      return renderFormItem(text, _objectSpread({
        mode: mode
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

export default FieldFromNow;